//
//  WBNavigationController.swift
//  weibo
//
//  Created by apple on 2017/5/17.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "第\(navigationController?.childViewControllers.count ?? 0)个"
        navigationBar.isHidden = true
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {

        //如果不是栈底控制器才需要隐藏，根控制器不需要处理

        if childViewControllers.count > 0 {
            //隐藏地步的tabbar
            viewController.hidesBottomBarWhenPushed = true
            //判断控制器的类型
            if let vc = viewController as? WBBaseViewController {
                //取出自定义的navitem
               var navTitle = "返回"
                
                if childViewControllers.count == 1 {
           //title 显示首页的标题
                    navTitle = childViewControllers.first?.title ?? "返回"
                    
                }
            vc.navItem.leftBarButtonItem = UIBarButtonItem(title: navTitle, fontSize: 16, normalColor: UIColor.gray, highlightedColor: UIColor.orange, target: self, action: #selector(popToParent))

            }

            
        }
        super.pushViewController(viewController, animated: true)

    }
    //pop返回到上一级控制器
    @objc private func popToParent(){
        popViewController(animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
